using Autodesk.Revit.UI;
using System;
using System.Windows.Media.Imaging;
using System.Reflection;
using System.IO;
using System.Linq;
using RevitForge.Services;

namespace RevitForge
{
    public class App : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication application)
        {
            try
            {
                string tabName = "RevitForge";
                application.CreateRibbonTab(tabName);
                // 获取或创建面板
                System.Collections.Generic.List<RibbonPanel> panels = application.GetRibbonPanels(tabName);
                RibbonPanel toolPanel = panels.FirstOrDefault(p => p.Name == "工具")
                    ?? application.CreateRibbonPanel(tabName, "工具");
                // 创建自定义面板
                RibbonPanel customPanel = panels.FirstOrDefault(p => p.Name == "自定义")
                    ?? application.CreateRibbonPanel(tabName, "自定义");
                // 添加刷新命令按钮
                PushButtonData refreshButton = new PushButtonData(
                    "RefreshCommands",
                    "刷新",
                    Assembly.GetExecutingAssembly().Location,
                    "RevitForge.Commands.RefreshCommand");
                refreshButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/RevitForge;component/Resources/refresh.png"));
                refreshButton.ToolTip = "刷新动态命令";
                toolPanel.AddItem(refreshButton);
                // 添加脚本运行按钮
                PushButtonData scriptRunnerButton = new PushButtonData(
                    "ScriptBotCommand",
                    "生成",
                    Assembly.GetExecutingAssembly().Location,
                    "RevitForge.Commands.ScriptBotCommand");
                scriptRunnerButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/RevitForge;component/Resources/script.png"));
                scriptRunnerButton.ToolTip = "生成并运行脚本";
                toolPanel.AddItem(scriptRunnerButton);

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show($"启动失败: {ex.Message}");
                return Result.Failed;
            }
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }
    }
}